/*
moxl - Modular XML Library
Copyright (c) 2007, Malcolm Sparks

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package moxl.dom.support;

import java.util.HashMap;
import java.util.Map;

import moxl.dom.MoxlAttribute;
import moxl.dom.QualifiedName;
import moxl.dom.XmlNamespace;

public class MoxlLocalAttributeGroup
{
	private final Map<String, MoxlAttribute> attributes = new HashMap<String, MoxlAttribute>();
	private final XmlNamespace namespace;

	public MoxlLocalAttributeGroup(XmlNamespace namespace)
	{
		this.namespace = namespace;
	}

	public boolean contains(String name)
	{
		return attributes.containsKey(name);
	}

	public void add(MoxlAttribute attribute)
	{
		if (!namespace.equals(attribute.getQName().getNamespace()))
		{
			throw new IllegalStateException("Namespace of this local group is " + namespace + ", namespace of given attribute is "
			        + attribute.getQName().getNamespace());
		}
		attributes.put(attribute.getQName().getLocalPart(), attribute);
	}

	public MoxlAttribute get(String name)
	{
		return attributes.get(name);
	}

	public MoxlAttribute remove(MoxlAttribute attribute)
	{
		QualifiedName name = attribute.getQName();
		if (!namespace.equals(name.getNamespace()))
		{
			throw new IllegalStateException("Namespace of this local group is " + namespace + ", namespace of given attribute is " + name.getNamespace());
		}
		return attributes.remove(name.getLocalPart());
	}

}
